package com.example.demo6.controller;

import com.example.demo6.entity.Goods;
import com.example.demo6.entity.Inventory;
import com.example.demo6.entity.Suppliers;
import com.example.demo6.service.GoodsService;
import com.example.demo6.service.InventoryService;
import com.example.demo6.service.SuppliersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Lizhi
 * @Date:Create：in 2023/12/5 11:08
 */
@RestController
@RequestMapping("/api")
public class apiController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    InventoryService inventoryService;
    @Autowired
    SuppliersService suppliersService;

    @RequestMapping("/suppliersDelete")
    public Integer suppliersDelete(@RequestParam String id) {
//
        System.out.println(id);
        Suppliers suppliers = new Suppliers();
        suppliers.setId(Integer.valueOf(id));
        if (suppliersService.delete(suppliers) == 1) {
            return 1;
        } else {
            return 0;
        }
    }
    @RequestMapping("/inventoryDelete")
    public Integer inventoryDelete(@RequestParam String id) {
//
        System.out.println(id);
        Inventory inventory = new Inventory();
        inventory.setId(Integer.valueOf(id));
        if (inventoryService.delete(inventory) == 1) {
            return 1;
        } else {
            return 0;
        }
    }


    @RequestMapping("/goodsDelete")
    public Integer goodsDelete(@RequestParam String id) {
        System.out.println("接收到的参数id值是:" + id);
        Goods goods = new Goods();
        goods.setId(Integer.valueOf(id));
//        1.调用service的删除方法
        if (goodsService.delete(goods) == 1) {
            return 1;
        } else {
            return 0;
        }
    }
    @RequestMapping("/suppliersUpdateProcess")
    public Integer suppliersUpdateProcess(@RequestParam Integer id,@RequestParam String name,@RequestParam String addr,@RequestParam String phone){
        Suppliers suppliers = new Suppliers();
        suppliers.setId(id);
        suppliers.setName(name);
        suppliers.setAddr(addr);
        suppliers.setPhone(phone);
        if(suppliersService.updata(suppliers)==1){
            return 1;
        }else {
            return 0;
        }
    }
    @RequestMapping("/inventoryUpdateProcess")
    public Integer inventoryUpdateProcess(@RequestParam Integer id,@RequestParam String name,@RequestParam String quantity){
        Inventory inventory = new Inventory();
        inventory.setId(id);
        inventory.setName(name);
        inventory.setQuantity(quantity);

        if(inventoryService.updata(inventory)==1){
            return 1;
        }else {
            return 0;
        }
    }

    //    处理更新的操作
    @RequestMapping("/goodsUpdateProcess")
    public Integer goodsUpdateProcess(@RequestParam Integer id, @RequestParam String name, @RequestParam double price
            , @RequestParam Integer quantity, @RequestParam String category,
                                     @RequestParam String supplier
    ) {
//        System.out.println();
        //      组装一个goods实体对象
//        Goods goods = new Goods(Integer.valueOf(id), name, Double.valueOf(price), Integer.valueOf(quantity), category, supplier);
        Goods goods = new Goods();
        goods.setId(Integer.valueOf(id));
        goods.setName(name);
        goods.setPrice(Double.valueOf(price));
        goods.setQuantity(Integer.valueOf(quantity));
        goods.setCategory(category);
        goods.setSupplier(supplier);
        System.out.println(goods);
        if(goodsService.updata(goods)==1){
            return 1;
        }else {
            return 0;
        }

}}
